{% extends 'base.html' %}
{% block css%}
<style type="text/css">
    .b1 {
        color: blue;
    }
</style>
{% endblock %}
{% block body%}
<div class="row">
    <div class="col-md-7">
        <form id="dlfile_form">
            <h4>Download</h4>
            <table class="table table-hover table-condensed form-inline">
                <tr>
                    <td>File Type</td>
                    <td>
                        <select  class="form-control" id="file_type" style="width:280px; height:30px" onchange="show_hide_item()">
                            <option value="fpga">fpga</option>
                            <option value="firmware">host firmware</option>
                            <option value="dsp">dsp</option>
                            <option value="cmis_config_table">cmis default table</option>
                            <option value="module_config_table">module config table</option>
                            <!-- <option value="module_firmware">module firmware</option> -->
                            <option value="module_firmware_dsp">module (firmware | firmware+dsp)</option>
                            <!-- <option value="hardware_info_table">hardware info table </option> -->
                            <option value="itla_firmware">itla firmware</option>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <span id="move_data" class="hide">
                            Move data to module
                            &nbsp;&nbsp;
                            <input class="form-control" type="checkbox" id="move_data_flag">
                        </span>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <!-- <input class="form-control" type="file" id="dlfile" name="dlfile" onchange="fileImport();"> -->
                        <input class="form-control" type="file" id="dlfile" name="dlfile">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="submit" id="btn_upload" class="btn btn-success">Submit</button>
                        &nbsp;&nbsp;
<!--                         <button type="button" id="btn_commit" class="btn btn-danger" disabled="disabled">Commit</button> -->
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <span id="upload">
                            <img src="{{url_for('static',filename='assets/upload.gif')}}" width="48">
                        </span>
                        <span id="fileinfo" style="font-weight: bolder; color:red; font-size: 20px"></span>
                        <br>
                        <div id="host-fw-info-box">
                        <span id="host-fw-info" style="color:blue"></span>
                        <br><br>
        <!--                 <button type="button" id="btn_commit1" class="btn btn-success">Host Firmware Commit</button> -->
                        </div>
                    </td>
                </tr>
            </table>
            <div class="progress">
                <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%" id="progress_val">
                    0%
                </div>
            </div>
        </form>
        <hr>
        <br>
        <table class="table table-condensed" id="cmd_btn_block">
            <tr>
                <td>
                    <button class="btn btn-success" id="only_move_data">
                        Only Move Data to Module
                        &nbsp;&nbsp;
                        <i id="only_move_data_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <button class="btn btn-primary" id="verify_data">Verify Data
                        &nbsp;&nbsp;
                        <i id="verify_data_loading"` class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>
                </td>
            </tr>
            <tr>
                <td style="font-weight:bolder; background-color: #FFFFCC">
                    <span id="cmd_btn_block_info">---</span>
                </td>
            </tr>
        </table>

        <table class="table table-condensed" id="cmd_btn_block1">
            <tr>
                <td>
                	<!-- sdvrc -->
                    <button class="btn btn-primary" id="download_module_firmware">
                    	Download <!-- start/download/verify -->
                        &nbsp;&nbsp;
                        <i id="download_module_firmware_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>

                    &nbsp;&nbsp;
                    <button class="btn btn-primary" id="run_module_firmware">
                    	Run
                        &nbsp;&nbsp;
                        <i id="run_module_firmware_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>

                    &nbsp;&nbsp;
                    <button class="btn btn-primary" id="commit_module_firmware">
                    	Commit
                        &nbsp;&nbsp;
                        <i id="commit_module_firmware_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>

                    &nbsp;
                    <button class="btn btn-primary" id="copy_module_firmware">
                        Copy
                        &nbsp;&nbsp;
                        <i id="copy_module_firmware_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>


      <!--               &nbsp;&nbsp;
                    <button class="btn btn-success" id="verify_module_file" title="Verify the consistency between the local file and the file downloaded to HOST Flash.">
                        Verify
                        &nbsp;&nbsp;
                        <i id="verify_module_file_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>
 -->
                    &nbsp;&nbsp;
                    <span class="pull-right">
                    
                    <button class="btn btn-warning" id="drc_module_firmware" title="One click to download, run and commit.">
                        Download + Run + Commit
                        &nbsp;&nbsp;
                        <i id="drc_module_firmware_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>
                    <input class="my_checkbox" type='checkbox' id='copy_image'> 
                    <span style='color:blue' title='If checked, copy firmware image. Image A and Image B will be upgraded automatically.'> Copy?</span>
                    </span>
                </td>
            </tr>
            <tr>
                <td style="font-weight:bolder;background-color: #FFFFCC">
                    <span style="font-weight: bold;" id="cmd_btn_block_info1">---</span>
                </td>
            </tr>
        </table>


        <div class="alert alert-warning" role="alert">
            <b>Warning!</b><br><br>
            Do NOT click other pages or other operations in any step of data processing, moving, verifying, otherwise the current operation may be terminated.
        </div>
    </div>

    <div class="col-md-5">
        <h4>Module Information</h4>
        <table class="table table-hover table-condensed">
            
        <div id="mod_info_txt">---</div>

        <tr>
            <td style="font-size: 12px" width="25%">
                <button class="btn btn-success" id="btn_get_mod_info">Get</button>
            </td>
            <td>
            </td>
        </tr>
        </table>


        <h4 class="{{ for_customer }}">Get CMIS Info</h4>
        <table class="{{ for_customer }} table table-hover table-condensed">
            <tr>
                <td style="font-size: 12px" width="25%">
                    <button class="btn btn-success" id="btn_get_cmis_info">
                        Get CMIS Info
                      &nbsp;&nbsp;
                      <i id="get_cmis_info_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>
                </td>
                <td>
                    <span id="dl_cmis_info">
                    Get data file 
                    &nbsp;&nbsp;
                    <a href="./static/files/cmis_info1.csv">
                    data1
                    </a>
                    &nbsp;&nbsp;
                    <a href="./static/files/cmis_info2.csv">
                    data2
                    </a>
                    </span>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <span id="cmis_info_ret" style="color: red"></span>
                </td>
            </tr>
        </table>


        <h4 class="{{ for_customer }}" >Compare module config table</h4>
        <form id="mct_form" method = "POST" enctype = "multipart/form-data">
        <table class="{{ for_customer }} table table-hover table-condensed form-inline">
            <tr>
                <td style="font-size: 12px" width="25%">
                    <input  class="form-control" type="file" id="mct_file" name="mct_file">
                </td>
                <td>
                    <input type="submit" class="btn btn-primary" id="btn_cmt_upload" value="Upload local file">
                </td>
            </tr>

            <tr>
                <td style="font-size: 12px" width="25%">
                    <button class="btn btn-success" id="btn_compare_mod_config_table">
                        Compare
                      &nbsp;&nbsp;
                      <i id="compare_mod_config_table_loading" class="fa fa-refresh fa-spin fa-fw"></i>
                    </button>

                    &nbsp;<i class='fa fa-info-circle' style='color: #ccc' title='For detail info, please see console window output.'></i>
                </td>
                <td>
                    <span id="mod_config_table_info" style="font-size: 16px">
                    </span>
                </td>
            </tr>

        </table>
    </form>
    </div>
</div>
{% endblock %}
{% block js%}
{{ super() }}
<script type="text/javascript">
var t;
var t1;
var n = 30;
var file_name;
var file_size;
var current_size = 0;
var REFRESSH_INTERVALS = 1 //seconds

function fileImport() {
    //get file object
    var selectedFile = document.getElementById('dlfile').files[0];
    file_name = selectedFile.name; //read file name
    file_size = selectedFile.size; //read file size
    console.log("filename: " + file_name + " size: " + file_size);
    // alert("filename: " + file_name + " size: " + file_size);

    file_status_update();
    // filename:osfp_evb_top_ver_9c34_crc16_1.bin size:2192014
    // var reader = new FileReader();
    // //reader.readAsText(selectedFile);//read file content
    // reader.onload = function () {
    //     console.log(this.result);
    // }
}

function file_status_update() {
    current_size = Math.round(current_size + (REFRESSH_INTERVALS / 0.085) * 200);
    file_status_percent = ((current_size / file_size) * 100).toFixed(2) + '%';

    console.log(file_status_percent);

    $("#progress_val").html(file_status_percent);
    $("#progress_val").attr("style", "width: " + file_status_percent);

    if (current_size > file_size) {
        file_status_percent = '98%';
        $("#progress_val").html(file_status_percent);
        $("#progress_val").attr("style", "width: " + file_status_percent);
        clearTimeout(t);
    } else {
        t = setTimeout(file_status_update, REFRESSH_INTERVALS * 1000);
    }
}



function show_hide_item() {
    var dl_type = $("#file_type").val();
    $("#host-fw-info-box").hide();
    $("#host-fw-info").html(''); //clean info

    //if(dl_type == 'dsp' || dl_type == 'module_firmware')
    if (dl_type == 'dsp') {
        $('#move_data').show();
        $('#cmd_btn_block').show();
        $('#cmd_btn_block1').hide();
    } 
    else if(dl_type == 'firmware')
    {
        $("#host-fw-info-box").show();
        $("#host-fw-info").html('<b>Hint:</b><br> 1. After the download is complete, please restart GUI.<br> 2. Please check host firmware version. <br>3. Click "Host Firmware Commit" button under the settings page to lock host firmware image.');
    }
    else if(dl_type == 'module_firmware' || dl_type == 'module_firmware_dsp')
    {
		$('#move_data').hide();
		$('#cmd_btn_block').hide();
		$('#cmd_btn_block1').show();
    } 
    else
    {
    	$('#move_data').hide();
        $('#cmd_btn_block').hide();
        $('#cmd_btn_block1').hide();
    }
}


$(document).ready(function() {

    $("#upload").hide();
    $("#btn_commit").hide();
    show_hide_item();
    $("#only_move_data_loading").hide();
    $("#verify_data_loading").hide();
    $("#copy_module_firmware_loading").hide();

	$("#download_module_firmware_loading").hide();
	$("#run_module_firmware_loading").hide();
	$("#commit_module_firmware_loading").hide();
    $("#drc_module_firmware_loading").hide();

    $("#compare_mod_config_table_loading").hide();
    // $("#verify_module_file_loading").hide();


    $("#get_cmis_info_loading").hide();
    $("#dl_cmis_info").hide();
    

    function btn_commit_show() {
        $("#btn_commit").show();
        $("#btn_commit").attr("disabled", true);
        $("#btn_commit").text("Commit (wait " + n + "s)");
        n = n - 1;

        if (n == 0) {
            $("#btn_commit").text('Commit');
            $("#btn_commit").attr("disabled", false);
            clearTimeout(t1);
            return false;
        }

        t1 = setTimeout(btn_commit_show, 1000);
    }

    $('#btn_get_mod_info').click(function() {
        url = "/api/btn_get_mod_info";
        $.get(url, function(data, status) {
            console.log(data)
            // $("#fileinfo").html("commit ok!");
            $("#mod_info_txt").html(data);
        });
    })


    $('#btn_get_cmis_info').click(function() {
        $("#get_cmis_info_loading").show();
        url = "/api/btn_get_cmis_info";
        $.get(url, function(data, status) {
            console.log(data);
            if(data == 'ok')
            {
                $("#get_cmis_info_loading").hide();
                $("#dl_cmis_info").show();
            }
            else
            {
                $("#get_cmis_info_loading").hide();
                $("#cmis_info_ret").html(data);
            }
        });
    })

    $('#btn_compare_mod_config_table').click(function() {
        $("#compare_mod_config_table_loading").show();
        url = "/api/compare_mod_config_table";
        $.get(url, function(data, status) {
            console.log(data);

            $("#compare_mod_config_table_loading").hide();
            if(data == "no_file")
            {
                 $("#mod_config_table_info").html("Please select local file to compare first!");
            }
            else
            {
                // $("#mod_config_table_info").html("<a href='./static/files/module_config_table_compare_file.txt'>Get compare info</a>");
                if(data=='Verify successfully.')
                    $("#mod_config_table_info").html("<font color=green>" + data + "</font>" + "&nbsp;<span style='color:green' class='glyphicon glyphicon-ok'></span>");
                else
                    $("#mod_config_table_info").html("<font color=red>" + data + "</font>");

            }

        });
    })

    
    $('#btn_commit').click(function() {
        url = "/api/commit";
        $.get(url, function(data, status) {
            console.log(data)
            // $("#fileinfo").html("commit ok!");
            $("#fileinfo").html(data);
        });
    });


    $('#btn_commit1').click(function() {
        url = "/api/commit";
        $.get(url, function(data, status) {
            console.log(data)
            // $("#fileinfo").html("commit ok!");
            $("#fileinfo").html(data);
        });
    });

    $('#only_move_data').click(function() {
        $("#only_move_data_loading").show();
        $("#cmd_btn_block_info").html("Moving data to module... ");

        url = "/api/only_move_data";
        $.get(url, function(data, status) {
            $("#only_move_data_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info").html("<font color=green>Move Data OK.</font>");
            else
                $("#cmd_btn_block_info").html("<font color=red>Move Data Error!</font>");
        });
    })

    $('#verify_data').click(function() {
        $("#verify_data_loading").show();
        $("#cmd_btn_block_info").html("Verifying data... ");

        url = "/api/verify_data";
        $.get(url, function(data, status) {
            $("#verify_data_loading").hide();
            console.log(data);
            $("#cmd_btn_block_info").html(data);
        });
    })



    //------------------------------------------------------------------------------------------
    $('#download_module_firmware').click(function() {
        $("#download_module_firmware_loading").show();
        $("#cmd_btn_block_info1").html("Downloading data to module ... ");

        url = "/api/download_module_firmware";
        $.get(url, function(data, status) {
            $("#download_module_firmware_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info1").html("<font color=green>Download OK and verify succeed. </font>");
            else
                $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
        });
    })


    $('#run_module_firmware').click(function() {
        $("#run_module_firmware_loading").show();
        $("#cmd_btn_block_info1").html("Run ... ");

        url = "/api/run_module_firmware";
        $.get(url, function(data, status) {
            $("#run_module_firmware_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info1").html("<font color=green>Run command has been sent.</font>");
            else
                $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
        });
    })

    $('#commit_module_firmware').click(function() {
        $("#commit_module_firmware_loading").show();
        $("#cmd_btn_block_info1").html("Commit ... ");

        url = "/api/commit_module_firmware";
        $.get(url, function(data, status) {
            $("#commit_module_firmware_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info1").html("<font color=green>Commit command has been sent.</font>");
            else
                $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
        });
    })

    $('#copy_module_firmware').click(function() {
        $("#copy_module_firmware_loading").show();
        $("#cmd_btn_block_info1").html("Copying ... ");

        url = "/api/copy_module_firmware";
        $.get(url, function(data, status) {
            $("#copy_module_firmware_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info1").html("<font color=green>The copy is complete.</font>");
            else
                $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
        });
    })

    

    $('#drc_module_firmware').click(function() {
        $("#drc_module_firmware_loading").show();
        $("#cmd_btn_block_info1").html("Processing ... ");

        if($("#copy_image").prop("checked") == true) copy_image=1; else copy_image=0;
        url = "/api/drc_module_firmware?copy_image=" + copy_image;

        $.get(url, function(data, status) {
            $("#drc_module_firmware_loading").hide();
            console.log(data);
            if (data == "ok")
                $("#cmd_btn_block_info1").html("<font color=green>Download/Run/Commit OK.</font>");
            else
                $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
        });
    })


    // $('#verify_module_file').click(function() {
    //     $("#verify_module_file_loading").show();
    //     $("#cmd_btn_block_info1").html("Verifying ... ");

    //     $.post("api/verify_module_file",
    //     {
    //         file_type:$("#file_type").val(),
    //     },
    //     function(data,status){
    //         $("#verify_module_file_loading").hide();
    //         console.log(data);
    //         if (data == "ok")
    //             $("#cmd_btn_block_info1").html("<font color=green>Verify OK.</font>");
    //         else
    //             $("#cmd_btn_block_info1").html("<font color=red>Error!</font>");
    //     });        
    // })
    



    //form Submit
    $("#dlfile_form").submit(function(evt) {
        // alert('download');
        if ($("#dlfile").val() == "") {
            $("#fileinfo").html("<font color=red>Please select a file first !</font>");
            return false;
        }

        //$("#upload").show();
        fileImport();

        $("#fileinfo").html("&nbsp;&nbsp;<font color=green>Please wait, processing ...</font>");
        //evt.preventDefault();
        var file_type = $("#file_type").val();
        var move_data_flag = ($("#move_data_flag").is(':checked')) ? 1 : 0;


        var formData = new FormData($(this)[0]);
        $.ajax({
            url: '/api/transfer?file_type=' + file_type + '&move_data_flag=' + move_data_flag,
            type: 'POST',
            data: formData,
            async: true,
            cache: false,
            contentType: false,
            enctype: 'multipart/form-data',
            processData: false,
            success: function(response) {
                //fileinfo = response.split(",")
                // alert(fileinfo[0] + ": " + fileinfo[1]);
                //$("#upload").hide();

                file_status_percent = '100%';
                $("#progress_val").html(file_status_percent + ", " + file_size);
                $("#progress_val").attr("style", "width: " + file_status_percent);
                $("#progress_val").attr("class", "progress-bar progress-bar-success");
                clearTimeout(t);

                //$("#fileinfo").html("File size: " + fileinfo[1] + " Type: " + fileinfo[2]);
                $("#fileinfo").html(response);

                // $("#btn_commit").show();
                if (file_type == 'firmware') {
                    btn_commit_show();
                }
            }
        });
        return false;
    });

    // mct form submit ;mct_file_upload
    $("#mct_form").submit(function(evt) {
        var formData = new FormData($(this)[0]);
        $.ajax({
            url: '/api/mct_file_upload',
            type: 'POST',
            data: formData,
            async: true,
            cache: false,
            contentType: false,
            enctype: 'multipart/form-data',
            processData: false,
            success: function(response) {
                // console.log("mct upload ok.")
                $("#mod_config_table_info").html("<font color=green>" + response + "</font>");
            }
        });
        return false;
    });



});
</script>
{% endblock %}